<?php
// +-----------------------+
// | 注释不留名，代码随便用 |
// +-----------------------+
namespace app\common\library\order;

use app\common\model\OrderModel;
use think\Exception;

/**
 * 职责链模式规范订单状态
 * Interface State
 */
abstract class State
{
    /**
     * @var self
     */
    protected $nextState;
    /**
     * @var OrderModel
     */
    protected $order;

    public function __construct(OrderModel $order)
    {
        $this->order = $order;
        $this->setNextState();
    }

    /**
     * 设置下一个职责状态
     * @return void
     */
    abstract public function setNextState();

    /**
     * 是否为当前职责
     * @return bool
     */
    abstract public function isDuty():bool ;

    public function action($op, ...$arg)
    {
        if($this->isDuty()) {
            //反射到 action目录下的对应形为类中
            if(!class_exists(__NAMESPACE__ . '\action\\' . $op)){
                new Exception(__NAMESPACE__ . '\action\\' . $op  .'类不存在');
            }
            $classes = __NAMESPACE__ . '\action\\' . $op;
            return call_user_func_array([new $classes($this->order), lcfirst(basename(str_replace('\\', '/', get_class($this))))], $arg);
        }
        if(isset($this->nextState) && !empty($this->nextState)){
            return $this->nextState->action($op, $arg);
        }
    }

}